//
package com.wanchuang.qhbb.push;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;


import com.wanchuang.qhbb.BuildConfig;
import com.wanchuang.qhbb.MainActivity;
import com.wanchuang.qhbb.adapter.JSbean;
import com.wanchuang.qhbb.module.main.MessageActivity;
import com.wanchuang.qhbb.utils.GsonUtils;

import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

//
///**
// * Created by intexh on 2016/7/7.
// * 自定义广播接收器
// */
//
public class JPushReceiver extends JPushMessageReceiver {
    private static final String TAG = "JPush";

    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageOpened(context, notificationMessage);
        Log.e("NotificationReceiver", notificationMessage.notificationExtras);
        if (isAppAlive(context, BuildConfig.APPLICATION_ID)) {
            Log.i("NotificationReceiver", "the app process is alive");
            Intent mainIntent = new Intent(context, MainActivity.class);
            mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Intent detailIntent;
            // 解析服务器返回的数据
            JSbean jSbean = GsonUtils.deSerializedFromJson(notificationMessage.notificationExtras, JSbean.class);
            switch (jSbean.getAction()) {
                case "system":   //系统消息，跳转系统消息详情
                case "system_all":   //系统消息，跳转系统消息详情
                    detailIntent = new Intent(context, MessageActivity.class);
                    break;
//                case "pd_cash"://提现消息
//                    detailIntent = WebViewActivity.startIntentActivity(context, WebApis.cashWithdrawal_detail + "?pdc_id=" + jSbean.getSub_action());
//                    break;
//                case "reward"://分润消息
//                case "refund"://返现消息
//                    detailIntent = new Intent(context, ZhangDanActivity.class);
//                    detailIntent.setType(String.valueOf(jSbean.getWallet_info().getWallet_type()));
//                    detailIntent.putExtra("tab",GsonUtils.serializedToJson(jSbean.getWallet_info()));
//                    detailIntent.putExtra("title","分润钱包");
//                    detailIntent.putExtra("push",true);
//                    break;
//                case "policy"://政策分配消息
//                    detailIntent = new Intent(context, PolicyActivity.class);
//                    detailIntent.putExtra("name", jSbean.getPolicy_info().getMember_name());
//                    detailIntent.putExtra("time", jSbean.getPolicy_info().getMember_mobile());
//                    detailIntent.putExtra("member_id", jSbean.getPolicy_info().getMember_id());
//                    break;
//                case "transfer"://划拨消息
//                    detailIntent = WebViewActivity.startIntentActivity(context, WebApis.transferApplication + "?transfer_id=" + jSbean.getSub_action());
//                    break;
                default:
                    detailIntent = new Intent(context, MainActivity.class);
            }
            Intent[] intents = {mainIntent, detailIntent};
            context.startActivities(intents);
        } else {
            Log.i("NotificationReceiver", "the app process is dead");
            Intent launchIntent = context.getPackageManager().
                    getLaunchIntentForPackage(BuildConfig.APPLICATION_ID);
            launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            launchIntent.putExtra("EXTRA_BUNDLE", notificationMessage.notificationExtras);
            Log.e("the app process is dead", notificationMessage.notificationExtras);
            context.startActivity(launchIntent);
        }
    }

    public boolean isAppAlive(Context context, String packageName) {
        if (packageName == null || "".equals(packageName)) {
            return false;
        }
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }

    }
}


